/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.client.ui;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.Column;
import redora.client.Persistable;
import redora.client.constants.RedoraConstants;
import redora.client.mvp.ClientFactory;

import java.util.logging.Logger;

import static redora.client.Persistable.Scope.List;

/**
 * Anchor style widget that allows lazy retrieval of typically a n-to-1 relationship.
 * When the related pojo is not in the cache, it will show a 'see details' anchor.
 * When clicked by the user, it will be fetched from the server.<br>
 * The widget uses the cache service and has a listeners on it's pojo. When
 * the pojo is updated elsewhere, the change will synced. This also implies that as
 * soon as somewhere the pojo is fetched, this widget will be updated.
 * @author Nanjing Redorange (www.red-orange.cn)
 */
public class LazyCell<T extends Persistable> extends AbstractCell<Long> {
    static Logger l = Logger.getLogger("LazyCell");

    static RedoraConstants redoraConstants = GWT.create(RedoraConstants.class);

    final ClientFactory clientFactory;
    final Class<? extends Persistable> masterClass;

    @Override
    public void render(Context context, Long value, SafeHtmlBuilder sb) {
        if (value == null) return;
        sb.appendHtmlConstant("<div>");
        sb.append(SafeHtmlUtils.fromString(clientFactory.getLocator().locateService(masterClass).useCache(value, List)
                ? clientFactory.getLocator().locateService(masterClass).cache(value).toListString()
                : redoraConstants.lazyCellDefault()));
        sb.appendHtmlConstant("</div>");
    }

    public static abstract class LazyColumn<T extends Persistable> extends Column<T, Long> {
        public LazyColumn(Class<? extends Persistable> masterClass) {
            super(new LazyCell<T>(masterClass));
        }
    }

    public LazyCell(Class<? extends Persistable> masterClass) {
        super("click");
        this.clientFactory = GWT.create(ClientFactory.class);
        this.masterClass = masterClass;
    }

    @Override
    public void onBrowserEvent(Context context, Element parent, Long value, NativeEvent event, ValueUpdater<Long> valueUpdater) {
        super.onBrowserEvent(context, parent, value, event, valueUpdater);
        if (value == null) return;
        if ("click".equals(event.getType())) {
            clientFactory.getLocator().locateService(masterClass).findById(value, List);
        }
    }
}
